﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Input;
using Subsonic.Data;
using MichMan.Utilities.Requests;
using System.Collections.Generic;
using MichMan.Utilities;

namespace Subsonic.ViewModels
{
    public class ArtistPivotViewModel : PivotViewModel
    {
        public ArtistPivotViewModel(string selctedArtistId)
        {
            Id = selctedArtistId;
        }

        public override IRequest LoadPivots()
        {
            GetIndexesRequest req = new GetIndexesRequest();
            req.RunRequestCompleted += new MichMan.Utilities.Requests.RunRequestCompletedEventHandler(getIndexes_RunRequestCompleted);
            req.RunRequestAsync();
            return req;
        }

        void getIndexes_RunRequestCompleted(object sender, MichMan.Utilities.Requests.RunRequestCompletedEventArgs e)
        {
            GetIndexesRequest getIndexes = (GetIndexesRequest)sender;

            if (getIndexes.Response == null || getIndexes.RequestStatus != RequestStatus.Succeeded)
            {
                // TODO:  Display error
                return;
            }

            Index index = getIndexes.Response.Indexes.Indecies.First(i => i.Artist.Any(a => a.Id == Id));

            foreach (Artist artist in index.Artist.Pivot(a => a.Id == Id))
            {
                Pivots.Add(new ArtistAlbumItemsViewModel(artist));
            }
        }
    }
}
